package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Page<Dish> selectAllByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Dish> dishes = dishMapper.selectAllByPage(name);

        PageInfo<Dish> pageInfo = new PageInfo<>(dishes);

        Page<Dish> dishPage = new Page<>();
        dishPage.setRecords(pageInfo.getList());
        dishPage.setTotal(pageInfo.getTotal());

        return dishPage;
    }

    @Override
    public Boolean update(Dish dish) {
        Integer update = dishMapper.update(dish);
        return update>0;
    }

    @Override
    public Dish selectById(Long ids) {
        Dish dish = dishMapper.selectById(ids);
        return dish;
    }

    @Override
    public Boolean selectCountByCategoryId(Long categoryId) {
        Integer integer = dishMapper.selectCountByCategoryId(categoryId);
        return integer>0;
    }

    @Override
    public Boolean updateAll(DishDto dishDto) {
        dishMapper.update(dishDto);
        for (DishFlavor flavor : dishDto.getFlavors()) {
            dishFlavorMapper.delete(flavor.getId());
        }
        for (DishFlavor flavor : dishDto.getFlavors()) {
            dishFlavorMapper.addFlavor(flavor);
        }

        return true;
    }

    @Override
    public Boolean deleteByIds(Long[] ids) {
        Integer integer = dishMapper.deleteByIds(ids);
        return integer>0;
    }

    @Override
    public Boolean updateStatus(Integer status,Long[] ids) {
        Integer integer = dishMapper.updateStatus(status, ids);

        return integer>0;
    }

    @Transactional
    @Override
    public Boolean add(DishDto dishDto) {
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);

        dishMapper.add(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
        }

        Integer integer = dishFlavorMapper.addDishFlavor(flavors);
        return true;
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.findByCategoryId(categoryId);
        return dishList;
    }

    @Override
    public List<DishDto> findByCategoryIdAndStatus(Long categoryId, Integer status) {
        List<Dish> dishes = dishMapper.findByCategoryIdAndStatus(categoryId, status);

        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish : dishes) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            List<DishFlavor> flavors = dishFlavorMapper.findFlavorsByDishID(dish.getId());
            dishDto.setFlavors(flavors);

            dishDtos.add(dishDto);
        }
        return dishDtos;
    }


}
